package com.cloudfun.campusshare.controller;

import com.cloudfun.campusshare.common.constant.Code;
import com.cloudfun.campusshare.common.exception.BusinessException;
import com.cloudfun.campusshare.common.model.dto.BatchOperateDTO;
import com.cloudfun.campusshare.common.model.vo.MerchantFIDetailVO;
import com.cloudfun.campusshare.common.model.vo.OMDataOverviewVO;
import com.cloudfun.campusshare.common.model.vo.OpMerchantExportVO;
import com.cloudfun.campusshare.common.model.vo.OpMerchantListVO;
import com.cloudfun.campusshare.common.response.BaseResult;
import com.cloudfun.campusshare.common.response.BaseResultFactory;
import com.cloudfun.campusshare.common.response.PageResult;
import com.cloudfun.campusshare.common.security.AuthRequired;
import com.cloudfun.campusshare.common.security.AuthenticationHolder;
import com.cloudfun.campusshare.service.DeviceOrderService;
import com.cloudfun.campusshare.service.DeviceService;
import com.cloudfun.campusshare.service.UserService;
import com.cloudfun.campusshare.util.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by Huqin on 2020/5/8
 */
@Slf4j
@RestController
@RequestMapping("/v1/op-merchant")
@Api(value = "运营商模块", description = "运营商模块")
@AuthRequired
public class OperationalMerchantController {

    @Autowired
    private UserService userService;

    @Autowired
    private DeviceOrderService deviceOrderService;

    @Autowired
    private DeviceService deviceService;

    @GetMapping("/list")
    @ApiOperation("运营商家列表")
    @AuthRequired
    BaseResult<PageResult<OpMerchantListVO>> opMerchantList(Pageable pageable, @ApiParam(value = "搜索关键字") @RequestParam(required = false) String value) {
        return BaseResultFactory.produceResult(Code.SUCCESS, userService.searchOpMerchants(value, pageable));
    }

    @ApiOperation(value = "导出运营商家列表")
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    @AuthRequired
    BaseResult<?> omListExport(HttpServletResponse response, @ApiParam(value = "BatchOperateDTO", required = true) BatchOperateDTO<String> batchOperateDTO) throws IOException {
        if (CollectionUtils.isEmpty(batchOperateDTO.getIds()) && StringUtils.isBlank(batchOperateDTO.getSearchData()) && !BooleanUtils.isTrue(batchOperateDTO.getAll())) {
            return BaseResultFactory.produceResult(Code.PARAMETER_ERROR, "参数错误");
        }
        long start = System.currentTimeMillis();
        List<OpMerchantExportVO> exportVOList;
        if (!CollectionUtils.isEmpty(batchOperateDTO.getIds())) {
            exportVOList = userService.exportOpMerchant(batchOperateDTO.getIds(), null);
        } else if (null != batchOperateDTO.getSearchData()) {
            exportVOList = userService.exportOpMerchant(null, batchOperateDTO.getSearchData());
        } else {
            exportVOList = userService.exportOpMerchant(null, null);
        }
        log.debug("导出{}个运营商家信息记录耗时：{}ms", exportVOList.size(), (System.currentTimeMillis() - start));
        ExcelUtils.exportExcel(exportVOList, null, "运营商信息", OpMerchantExportVO.class, "运营商信息", response);
        return BaseResultFactory.produceEmptyResult(Code.SUCCESS);
    }

    @GetMapping("/detail")
    @ApiOperation("运营商家详情")
    @AuthRequired
    BaseResult<MerchantFIDetailVO> opMerchantDetail(@ApiParam(value = "运营商id", required = true) @RequestParam String opMerchantId) {
        return BaseResultFactory.produceResult(Code.SUCCESS, userService.omDetail(opMerchantId));
    }

    @GetMapping("/overview")
    @ApiOperation("运营情况")
    @AuthRequired
    BaseResult<OMDataOverviewVO> operationalMerchantDataOverview(@ApiParam(value = "运营商id，管理员操作的话需要传")
                                                                 @RequestParam(required = false) String opMerchantId) {
        if (AuthenticationHolder.isAdmin() && StringUtils.isBlank(opMerchantId)) {
            throw new BusinessException(Code.PARAMETER_ERROR);
        }
        OMDataOverviewVO omDataOverviewVO = new OMDataOverviewVO();
        String orderTotal = deviceOrderService.orderTotal(opMerchantId);
        omDataOverviewVO.setOrderTotal(orderTotal);

        Long countInteractedDevices = deviceService.countInteractedDevices(opMerchantId);
        omDataOverviewVO.setInteractedDeviceNum(countInteractedDevices);

        Long countTodayInteractedDevices = deviceService.countTodayInteractedDevices(opMerchantId);
        omDataOverviewVO.setTodayInteractedDeviceNum(countTodayInteractedDevices);

        return BaseResultFactory.produceResult(Code.SUCCESS, omDataOverviewVO);
    }

}
